<template>
    <div>
        <div @click="popup.openPopup" class="vue-flow-model">
            <n-icon size="30" color="#18A058">
                <DataReference/>
            </n-icon>
            <Handle id="DataReplaced-inlet" type="target" :position="Position.Left"
                    :is-valid-connection="props.isValidSourcePos"/>
            <Handle id="DataReplaced-export" type="source" :position="Position.Right"
                    :is-valid-connection="props.isValidTargetPos"/>
        </div>
        <span>数据替换</span>
        <flow-popup-window
            :show="popup.show"
            :on-click-outside="popup.clickPopupOutside"
            :x="popup.x"
            :y="popup.y"
        >
            <n-card title="数据替换">
                <p>选择列： </p>
                <n-select
                    v-model:value="field"
                    :options="props.data.data.title"
                    multiple
                    @update:value="popup.reopen"
                    placeholder="选择列"
                    label-field="title"
                    value-field="key"
                />
                <p class="margin">替换方式： </p>
                <n-select
                    v-model:value="alternative"
                    :options="alternativeArray"
                    @update:value="popup.reopen"
                />
                <div v-show="alternative==='text'">
                    <p>被替换文本: </p>
                    <n-input
                    v-model:value="old_text"
                    placeholder="旧文本"
                    />
                    <p>替换为: </p>
                    <n-input
                        v-model:value="new_text"
                        placeholder="新文本"
                    />
                </div>
                <div v-show="alternative === 'position'">
                    <p>起始字符位置: </p>
                    <n-input-number
                        v-model:value="start_character"
                        placeholder="位置"
                    />
                    <p>替换字符数: </p>
                    <n-input-number
                        v-model:value="start_character_number"
                        placeholder="字符数"
                    />
                    <p>替换为: </p>
                    <n-input
                        v-model:value="new_text"
                        placeholder="新文本"
                    />

                </div>
                <template #footer>
                    <n-space>
                        <n-button @click="getSuperiorData">获取上级节点数据</n-button>
                        <n-button @click="replaced">添加</n-button>
                    </n-space>
                </template>
            </n-card>
        </flow-popup-window>
    </div>
</template>

<script setup>

import {Handle, Position} from "@vue-flow/core";
import {DataReference} from "@vicons/carbon";
import {useElementsStore} from "@/stores/elements";
import workApi from "@/http/workApi";
import FlowPopupWindow from "@/components/VueFlowModel/vueFlowPopupWindow/FlowPopupWindow.vue";

const props = defineProps(['isValidTargetPos', 'isValidSourcePos', 'id', 'data'])
const elements = useElementsStore()
// 点击弹窗
const popup = reactive({
    show: false,
    x: null,
    y: null,
    wait: false,
    clickPopupOutside: () => {
        popup.closedPopup()
    },
    openPopup: (e) => {
        popup.x = e.x
        popup.y = e.y
        popup.show = true
    },
    closedPopup: () => {
        if (!popup.wait) {
            popup.show = false;
        }
    },
    reopen: () => {
        popup.show = true
    }
})
const field = ref([])
const alternativeArray = [
    {label:"文本替换",value:"text"},
    {label:"位置替换",value:"position"},
]
const alternative = ref("text")
const replaced = ()=>{
    if (field.value.length === 0) {
        window.$message.warning("还没选列")
        return
    }
    if (alternative.value === 'text') {
        replacedTextApi();
    }else {
        replacedPositionApi();
    }
}
const old_text = ref("")
const new_text = ref("")
const replacedTextApi = ()=>{
    if (old_text.value === '') {
        window.$message.warning("还没有填写完")
        return
    }
    workApi.post("perform_tasks/replace_text",{
        data: props.data.data,
        operation: {
            row: {serial_number: []},
            column: {
                serial_number: field.value,
                old_text:old_text.value,
                new_text:new_text.value
            }
        }
    }).then(r=>{
        props.data.data = r.data
    })
}
const start_character = ref(0)
const start_character_number = ref(0)
const replacedPositionApi = ()=>{
    workApi.post("perform_tasks/replace_position",{
        data: props.data.data,
        operation: {
            row: {serial_number: []},
            column: {
                serial_number: field.value,
                start_character:start_character.value,
                number:start_character_number.value,
                new_text:new_text.value
            }
        }
    }).then(r=>{
        props.data.data = r.data
    })
}

const getSuperiorData = () => {
    // 获取上级节点数据
    for (let i of elements.elements) {
        if (i['target'] === props.id) {
            for (let j of elements.elements) {
                if (j.id === i['source']) {
                    props.data.data = {...j.data.data}
                    props.data.data.title.map(s => {
                        // 移除可修改
                        delete s.render
                        return s
                    })
                    return
                }
            }
        }
    }
}

</script>

<style scoped lang="less">
.n-select {
    width: 200px;
}
</style>